package org.example.base;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;

/**
 * @Author codeYang
 * @Date 2025/3/16 17:37
 * @Version 1.0
 */
public class StreamingDemo {

    public static void main(String[] args) {
        OpenAiStreamingChatModel streamingChatModel = OpenAiStreamingChatModel
                .builder()
                .baseUrl("http://langchain4j.dev/demo/openai/v1")
                .modelName("gpt-4o-mini")
                .apiKey("demo")
                .build();

        streamingChatModel.generate("你叫什么?", new StreamingResponseHandler<AiMessage>() {
            @Override
            public void onNext(String token) {
                System.out.println(token);

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onError(Throwable error) {
                System.out.println(error);
            }
        });

    }
}
